package com.sakhalin.news.ui.main

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.sakhalin.news.BottomNavigationBar
import com.sakhalin.news.CommentsPage
import com.sakhalin.news.SettingsPage
import com.sakhalin.news.TopBar
import com.sakhalin.news.models.main.NavigationItem
import com.sakhalin.news.ui.auth.AuthorizationPage
import com.sakhalin.news.ui.news.NewsDetailsPages
import com.sakhalin.news.ui.news.NewsPage
import com.sakhalin.news.ui.theme.SakhalinNewsTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SakhalinNewsTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
                    MainScreen()
                }
            }
        }
    }
}

@Composable
fun MainScreen() {
    val navController = rememberNavController()

    Scaffold(
        topBar = { TopBar() },
        bottomBar = { BottomNavigationBar(navController) },
        content = { padding ->
            Box(modifier = Modifier.padding(padding)) {
                Navigation(navController = navController)
            }
        },
        backgroundColor = Color.White
    )
}

@Composable
fun Navigation(navController: NavHostController) {
    NavHost(navController, startDestination = NavigationItem.News.route) {
        composable(NavigationItem.News.route) {
            NewsPage(navController)
        }
        composable(NavigationItem.Comments.route) {
           CommentsPage()
        }
        composable(NavigationItem.Settings.route) {
            SettingsPage()
        }
        composable("news/{newsId}"){
            NewsDetailsPages(it.arguments?.getString("newsId")?:"0")
        }
    }
}


@Preview(showBackground = true)
@Composable
fun MainScreenPreview() {
    MainScreen()
}


